<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Flashgame
 */
?>

<?php if( !$this->flashgame): ?>
<?php echo $this->translate('The game you are looking for does not exist or has been deleted.');?>
<?php return; // Do no render the rest of the script in this mode
endif; ?>
<?php if ($this->flashgame->type == '1'): ?>
<?php
$this->headScript()                                                      
    ->appendFile($this->baseUrl() . '/externals/swfobject/swfobject.js');
?>
<?php endif; ?>
<?php
if (!$this->flashgame->closed && $this->flashgame->type == '1')
{
?>
    <script type="text/javascript">
    <!--
    swfobject.embedSWF(
        "<?php echo $this->game_location;?>",
        "id_flashgame",
        "<?php echo $this->flashgame->width ? $this->flashgame->width : 720;?>",
        "<?php echo $this->flashgame->height ? $this->flashgame->height : 568;?>",
        "9.0.0",
        "expressInstall.swf"
    );
    -->
    </script>
<?php
}
?>
<script type="text/javascript">
var categoryAction =function(category)
{
    $('category').value = category;
    $('filter_form').submit();
}
  var tagAction = function(tag){
    $('page').value = 1;
    $('tag').value = tag;
    $('filter_form').submit();
  }
    var img_star_full = "application/modules/Flashgame/externals/images/star_full.png";
    var img_star_partial = "application/modules/Flashgame/externals/images/star_part.png";
    var img_star_none = "application/modules/Flashgame/externals/images/star_none.png";  
    
    function rating_mousehover(rating) {
        for(var x=1; x<=5; x++) {
          if(x <= rating) {
            $('rate_'+x).src = img_star_full;
          } else {
            $('rate_'+x).src = img_star_none;
          }
        }
    }

    function rating_mouseout() {
        for(var x=1; x<=5; x++) {
          if(x <= <?php echo $this->flashgame->rates ?>) {
            $('rate_'+x).src = img_star_full;
          } else if(<?php echo $this->flashgame->rates ?> > (x-1) && x > <?php echo $this->flashgame->rates ?>) {
            $('rate_'+x).src = img_star_partial;
          } else {
            $('rate_'+x).src = img_star_none;
          }
        }
    }
    function rate(rates){
        $('flashgame_rate').onmouseout = null;
        window.location = en4.core.baseUrl + 'games/rate/<?php echo $this->flashgame->getIdentity();?>/'+rates;
        //en4.core.request.send(new Request.JSON(
        //{       
        //        url : 'http://localhost' + en4.core.baseUrl + 'games/rate',       
        //        data : {
        //          format     : 'json',
        //          'flashgame_id' : '<?php echo $this->flashgame->getIdentity();?>',
        //          'rates' : rates     
        //        },
        //        //onSuccess : window.location = forward_link
        //}
        //)); 
      }
  
</script>

<div class='layout_right'>
  <div class='flashgames_gutter'>
    <div class='flashgames_gutter_photo'>
      <?php echo $this->htmlLink($this->user($this->flashgame->owner_id)->getHref(), $this->itemPhoto($this->user($this->flashgame->owner_id))) ?>
    </div>
    <a href='<?php echo $this->url(array('id' => $this->flashgame->owner_id), 'user_profile') ?>' class="flashgames_gutter_name"><?php echo $this->user($this->flashgame->owner_id)->getTitle() ?></a>
    <ul class='flashgames_gutter_options'>

      <?php if ($this->can_edit):?>
        <li>
          <a href='<?php echo $this->url(array('flashgame_id' => $this->flashgame->flashgame_id), 'flashgame_edit', true) ?>' class='buttonlink icon_flashgame_edit'><?php echo $this->translate('Edit This Game');?></a>
        </li>
        <?php endif; ?>
        <?php if ($this->can_delete):?>
        <li>
          <a href='<?php echo $this->url(array('flashgame_id' => $this->flashgame->flashgame_id), 'flashgame_delete', true) ?>' class='buttonlink icon_flashgame_delete'><?php echo $this->translate('Delete Game');?></a>
        </li>
      <?php endif; ?>
    </ul>

    <form id='filter_form' class='global_form_box' method='post' action='<?php echo $this->url(array('module' => 'flashgame', 'controller' => 'index', 'action' => 'index'), 'default', true) ?>' style='display: none;'>
          <input type="hidden" id="tag" name="tag" value=""/>
          <input type="hidden" id="page" name="page" value=""/>
          <input type="hidden" id="category" name="category" value=""/>
          <input type="hidden" id="start_date" name="start_date" value="<?php if ($this->start_date) echo $this->start_date;?>"/>
          <input type="hidden" id="end_date" name="end_date" value="<?php if ($this->end_date) echo $this->end_date;?>"/>
    </form>

    <?php if (count($this->userCategories )):?>
      <h4><?php echo $this->translate('Categories');?></h4>
      <ul>
          <li> <a href='javascript:void(0);' onclick='javascript:categoryAction(0);'><?php echo $this->translate('All Categories');?></a></li>
          <?php foreach ($this->userCategories as $category): ?>
            <li> <a href='javascript:void(0);' onclick='javascript:categoryAction(<?php echo $category->category_id?>);'><?php echo $this->translate($category->category_name) ?></a></li>
          <?php endforeach; ?>
      </ul>
    <?php endif; ?>

    <?php
    $this->tagstring = "";
    if (count($this->userTags )){
      foreach ($this->userTags as $tag){
        if (!empty($tag->text)){
          $this->tagstring .= " <a href='javascript:void(0);' onclick='javascript:tagAction({$tag->tag_id})' >#$tag->text</a> ";
        }
      }
    }
    ?>

    <?php if ($this->tagstring ):?>
      <h4><?php echo $this->translate('%1$s\'s Tags', $this->user($this->flashgame->owner_id)->getTitle())?></h4>
      <ul>
        <?php echo $this->tagstring;?>
      </ul>
    <?php endif; ?>

  </div>
</div>

<div class='layout_middle'>
  <h2>
    <?php echo $this->translate('%1$s\'s Game', $this->htmlLink($this->owner->getHref(), $this->owner->getTitle()))?>
  </h2>
  <ul class='flashgames_entrylist'>
    <li>
      <h3>
        <?php echo $this->flashgame->getTitle() ?>
      </h3>
      <?php if ($this->flashgame->closed == 1):?>
        <br />
        <div class="tip">
          <span>
            <?php 
                echo $this->translate('This game is waiting for approval.');
            ?>
          </span>
        </div>
        <br/>
      <?php endif; ?>

      <div class="flashgame_entrylist_entry_date">
        <?php echo $this->translate('Posted by');?> <?php echo $this->htmlLink($this->flashgame->getParent(), $this->flashgame->getParent()->getTitle()) ?>
        <?php echo $this->timestamp($this->flashgame->creation_date) ?>
        <?php if ($this->category):?>- <?php echo $this->translate('Filed in');?> <a href='javascript:void(0);' onclick='javascript:categoryAction(<?php echo $this->category->category_id?>);'><?php echo $this->translate($this->category->category_name) ?></a> <?php endif; ?>
        <?php if (count($this->flashgameTags )):?>
        -
          <?php foreach ($this->flashgameTags as $tag): ?>
          <?php if (!empty($tag->getTag()->text)):?>
            <a href='javascript:void(0);' onclick='javascript:tagAction(<?php echo $tag->getTag()->tag_id; ?>);'>#<?php echo $tag->getTag()->text?></a>&nbsp;
          <?php endif; ?>
          <?php endforeach; ?>
        <?php endif; ?>

        <?php echo $this->fieldValueLoop($this->flashgame, $this->fieldStructure) ?>
            <?php /*
        <?php foreach ($this->fieldsByAlias as $key => $value): ?>
          <?php if($value):?>
            <?php if($key == "price"):?>
              <br/><?php echo ucfirst($key); ?>:
              <?php echo Engine_Api::_()->getApi('settings', 'core')->flashgame_currency . $this->locale()->toCurrency($value) ?><br/>
            <?php else:?>
              <br/><?php echo ucfirst($key); ?>: <?php echo $value;?>
            <?php endif; ?>
          <?php endif; ?>
        <?php endforeach; ?>
             *
             */ ?>

      </div>
      <div class="flashgame_entrylist_entry_body">
        <?php echo $this->flashgame->body ?>
      </div>
	  
	  <?php if (!$this->flashgame->closed): ?>
        <ul class='flashgame_video' id="id_flashgame">
			<?php if ($this->flashgame->type == '2'): ?>
				<?php echo $this->flashgame->embed_script;?>
			<?php endif;?>
		</ul>
	  <?php else: ?>
		<?php if ($this->viewer->getIdentity() && $this->viewer->level_id < 2): ?>
			<ul class='flashgame_video' id="id_flashgame">
				<?php if ($this->flashgame->type == '2'): ?>
					<?php echo $this->flashgame->embed_script;?>
				<?php endif;?>
			</ul>
		<?php endif; ?>
	  <?php endif; ?>	
    </li>
  </ul>
  <a name="ratearea"></a>
  <div  <?php if ($this->can_rate): ?> onmouseout="rating_mouseout()" <?php endif;?> id="flashgame_rate">
    <?php for($i = 1; $i <= 5; $i++): ?>
      <img width="20" id="rate_<?php print $i;?>" <?php if ($this->can_rate): ?>  style="cursor: pointer;" onclick="rate(<?php echo $i; ?>);" onmouseover="rating_mousehover(<?php echo $i; ?>);"<?php endif; ?> src="application/modules/Flashgame/externals/images/<?php if ($i <= $this->flashgame->rates): ?>star_full.png<?php elseif( $i > $this->flashgame->rates &&  ($i-1) <  $this->flashgame->rates): ?>star_part.png<?php else: ?>star_none.png<?php endif; ?>" />
    <?php endfor; ?>
  </div>
  <?php echo $this->action("list", "comment", "core", array("type"=>"flashgame", "id"=>$this->flashgame->getIdentity())) ?>
</div>